<?php

/**
 *  供应商/零售商
*/
namespace app\admin\service;
use app\admin\model\SellerInfo as ModelSellerInfo;
use app\admin\model\SellerProduct as ModelSellerProduct;
use app\facade\Basic;

class SellerProduct extends BaseService
{


    /**
     *   获取枚举
    */
    public function getEnum($param)
    {
       $ModelSellerProduct = new ModelSellerProduct();
       $result = $ModelSellerProduct->getFieldEnum($param['field']);
       return Basic::data(0,'ok',$result);
    }

    /**
     * 获取机型列表
     */
    public function getList($param)
    {

        /* 状态筛选 */
        if (!empty($param['status'])) {
            $where[] = ['status','=',$param['status']];
        }

        /* 机型筛选 */
        if (!empty($param['model_id'])) {
            $where[] = ['model_id', '=',$param['model_id']];
        }

        /* 商品名称搜索 */
        if (!empty($param['product_name'])) {
            $param['product_name'] = trim($param['product_name']);
            $where[] = ['product_name', 'like', '%' . $param['product_name'] . '%'];
        }

        /* 查询所有 */
        if (!empty($param['category'])) {
            if (!empty($param['seller_id'])) {
                $where[] = ['seller_id', '=',$param['seller_id']];
            } else {

                $category[] = $param['category'];
                $category[] = 3;

                $seller =  ModelSellerInfo::where('category','in',$category)
                    ->where('is_del','=','0')
                    ->field('id')
                    ->select();

                if ( $seller ) {
                    $seller_ids = array_column( $seller->toArray(),'id');
                    $where[] = ['seller_id', 'in',$seller_ids];
                }
            }
        }

        $result = ModelSellerProduct::with([
            'seller_name' => function($query){
                return $query->field('id,name');
            },
            'model_name' => function ($query) {
                return $query->field('id,model_name');
            }
        ])->where($where)
            ->field('id,seller_id,model_id,model_id,product_name,product_detail_url,create_time,status')
            ->order('id', 'desc')
            ->paginate([
                'list_rows'=> $param['pageSize'],
                'var_page' => 'pageIndex',
            ]);
        $data = $result->toArray();
        foreach ( $data['data'] as $key =>$value ) {

            /* 渠道商 */
            if(!empty($value['seller_name'])) {
                $data['data'][$key]['seller_name'] = $value['seller_name']['name'];
            } else {
                $data['data'][$key]['seller_name'] = '';
            }

            /* 矿机型号 */
            if(!empty($value['model_name'])) {
                $data['data'][$key]['model_name'] = $value['model_name']['model_name'];
            } else {
                $data['data'][$key]['model_name'] = '';
            }
        }

        return Basic::paginator(0, 'ok', $data);
    }


    /**
     *  商品状态更新
    */
    public function status($param)
    {

        $result = ModelSellerProduct::where('id','in', $param['ids'])->field('id,status')->find();
        if (!$result) {
            return Basic::data(1, 'ID 不存在');
        }

        $result = ModelSellerProduct::where('id','in', $param['ids'])->update(['status'=> $param['status']]);
        if ($result) {
            return Basic::data(0, '更新成功');
        } else {
            return Basic::data(1, '更新失败');
        }
    }


    /**
     *  修改机型
     */
    public function update($param)
    {
        $param['product_name'] = trim($param['product_name']);
        /* 商品是否存在 */
        if (ModelSellerProduct::where('product_name', '=', $param['product_name'])->where('id','!=', $param['id'])->find()) {
            return Basic::data(1, '商品已存在');
        }

        $result = ModelSellerProduct::where('id','=', $param['id'])->find();
        if (!$result) {
            return Basic::data(1, 'ID 不存在');
        }

        $result->seller_id = $param['seller_id'];
        $result->model_id = $param['model_id'];
        $result->product_name = $param['product_name'];
        $result->product_detail_url = $param['product_detail_url'];
        $result->status = (int) $param['status'];

        if ($result->save()) {
            $this->logger->logOperation($result->id, 'SellerProduct', '修改', '修改商品');
            return Basic::data(0, '修改成功');
        } else {
            return Basic::data(1, '修改失败');
        }
    }


    /**
     *  获取详情
    */
    public function detail($param)
    {

        $result = ModelSellerProduct::with([
            'seller_name' => function($query){
                return $query->field('id,name');
            },
            'model_name' => function ($query) {
                return $query->field('id,model_name');
            }
        ])->where('id','=',$param['id'])
            ->field('id,seller_id,model_id,model_id,product_name,product_detail_url,create_time,status')
            ->order('id', 'desc')
            ->find();

        if (!$result) {
            return Basic::data(1, '商品不存在');
        }

        /* 渠道商 */
        if(!empty($result->seller_name)) {
            $result->seller_name = $result->seller_name->name;
        } else {
            $result->seller_name = '';
        }


        /* 矿机型号 */
        if(!empty($result->model_name)) {
            $result->model_name = $result->model_name->model_name;
        } else {
            $result->model_name = '';
        }


        return Basic::data(0, 'ok', $result);
    }

    /**
     *   删除机型
    */
    public function delete($param)
    {

        $result = ModelSellerProduct::where('id','in',$param['ids'])->select();
        if(!$result) {
            return Basic::data(1, '未找到');
        }

        if ( $result ) {
            $result = ModelSellerProduct::where('id','in',$param['ids'])->delete();
            if ( $result ) {
                return Basic::data(0, '删除成功');
            } else {
                return Basic::data(1, '删除失败');
            }
        } else {
            return Basic::data(1, '删除失败');
        }
    }
}
